package com.china08.yxyapi.api.manage.tec.teaching.live.lessonManage;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.api.manage.tec.teaching.live.myClassSchedule.MyClassScheduleController;
import com.china08.yxyapi.entity.db.live.YxyLiveCatalog;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.live.YxyLiveCatalogRepository;
import com.china08.yxyapi.util.PageModel;

import io.swagger.annotations.ApiOperation;

/**
 * 我的教学-直播课-教师-直播课管理-直播课查询
 * @author 赵亚辉
 *
 */
@CrossOrigin
@RestController
public class LiveManageLessonFindController extends YxyApiBaseController{
	
	private static final Logger logger=Logger.getLogger(MyClassScheduleController.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	private static SimpleDateFormat sf1=new SimpleDateFormat("yyyy年MM月dd日");
	
	private static SimpleDateFormat sf2=new SimpleDateFormat("HH:mm");
	@Autowired
	private YxyLiveCatalogRepository yxyLiveCatalogRepository;
	
	@ApiOperation(value = "直播课查询")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/live/liveManageLesson/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PageModel<Map<String,Object>> find(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
		
		PageModel<Map<String,Object>> respModel=new PageModel<Map<String,Object>>();
		
		String teacherId = getProfile(request, response).getId();
		
		String countSQL="select count(1) ";
		String lessonDataSQL="select id,create_date,cover,catalog_id1,catalog_id2,catalog_id3,scope,count,summary,audit_status,is_visible,status,name ";

		String commmonSQL=" from yxy_live.yxy_live_lesson where teacher_id='"+teacherId+"'";
		Integer scope=reqModel.scope;
		if(scope!=null&&scope==0){
			commmonSQL+=" and scope="+scope;
		}
		Integer classStatus=reqModel.classStatus;
		//一会处理
		if(classStatus!=null&&classStatus!=110){
			
			// 0待审核  审核0  下架0  未开始0
			// 1未通过  审核2  下架0  未开始0
			// 2上架      审核1  上架1  未开始0、直播中1
			// 3下架     审核1  下架0 
			// 4结束     审核1  上架1  结束2
			
			if(classStatus==0){
				commmonSQL+=" and audit_status=0 and is_visible=0 and status=0";
			}else if(classStatus==1){
				commmonSQL+=" and audit_status=2";
			}else if(classStatus==2){
				commmonSQL+=" and audit_status=1 and is_visible=1 and status!=2";
			}else if(classStatus==3){
				commmonSQL+=" and audit_status=1 and is_visible=0";
			}else if(classStatus==4){
				commmonSQL+=" and audit_status=1 and is_visible=1 and status=2";
			}
		}
		String catalogId1=reqModel.catalogId1;
		if(StringUtils.isNotEmpty(catalogId1)){
			commmonSQL+=" and catalog_id1='"+catalogId1+"'";
		}
		String catalogId2=reqModel.catalogId2;
		if(StringUtils.isNotEmpty(catalogId2)){
			commmonSQL+=" and catalog_id2='"+catalogId2+"'";
		}
		String catalogId3=reqModel.catalogId3;
		if(StringUtils.isNotEmpty(catalogId3)){
			commmonSQL+=" and catalog_id3='"+catalogId3+"'";
		}
		String name=reqModel.name;
		if(StringUtils.isNotEmpty(name)){
			commmonSQL+=" and name like '%"+name+"%'";
		}
		
		lessonDataSQL+=commmonSQL;
		lessonDataSQL+=" order by create_date";
		
		Integer pageIndex=reqModel.pageIndex;
		Integer pageSize=reqModel.pageSize;
		lessonDataSQL+=" limit "+(pageIndex-1)*pageSize+","+pageSize;
				
		List<Map<String, Object>> mapList = jdbcTemplate.queryForList(lessonDataSQL);
		for(Map<String, Object> map:mapList){
			Date createDate=(Date) map.get("create_date");
			if(createDate!=null){
				String formatDate=sf1.format(createDate)+" "+sf2.format(createDate);
				map.put("formatDate", formatDate);
			}
			String catalogId1_q=(String) map.get("catalog_id1");
			if(StringUtils.isNotEmpty(catalogId1_q)){
				YxyLiveCatalog yxyLiveCatalog=yxyLiveCatalogRepository.findById(catalogId1_q);
				if(yxyLiveCatalog!=null){
					map.put("catalogName1", yxyLiveCatalog.getName());
				}
			}
			String catalogId2_q=(String) map.get("catalog_id2");
			if(StringUtils.isNotEmpty(catalogId2_q)){
				YxyLiveCatalog yxyLiveCatalog=yxyLiveCatalogRepository.findById(catalogId2_q);
				if(yxyLiveCatalog!=null){
					map.put("catalogName2", yxyLiveCatalog.getName());
				}
			}
			String catalogId3_q=(String) map.get("catalog_id3");
			if(StringUtils.isNotEmpty(catalogId3_q)){
				YxyLiveCatalog yxyLiveCatalog=yxyLiveCatalogRepository.findById(catalogId3_q);
				if(yxyLiveCatalog!=null){
					map.put("catalogName3", yxyLiveCatalog.getName());
				}
			}
			
			// auditStatus 0-待审核,1-审核通过,2-审核拒绝
			// isVisible 0-不显示(下架),1-可显示(上架)
			// status 课程状态0-未开始,1-直播中,2-已结束
			Integer auditStatus=(Integer) map.get("audit_status");
			Integer isVisible=(Integer) map.get("is_visible");
			Integer status=(Integer) map.get("status");
			if(auditStatus!=null&&isVisible!=null&&status!=null){
				// 自定义课程状态 方便判断
				// classStatus 0：待审核;1:未通过；2，已上架；3，已下架；4，已结束
				Integer classStatus_item=null;
				if(auditStatus==0){
					classStatus_item=0;
				}else if(auditStatus==2){
					classStatus_item=1;
				}else if(auditStatus==1){
					if(isVisible==0){
						classStatus_item=3;
					}else if(isVisible==1){
						if(status==2){
							classStatus_item=4;
						}else{
							classStatus_item=2;
						}
					}
				}
				if(classStatus_item!=null){
					map.put("classStatus", classStatus_item);
				}
			}
		}
		
		countSQL+=commmonSQL;
		Integer totalCount = jdbcTemplate.queryForObject(countSQL, Integer.class);
		
		respModel.setPage(pageIndex,mapList,totalCount);
		
		return respModel;
	}
	
	protected static class ReqModel extends YxyApiReqModel{
		
		// 开设范围 0：本校；1：本区域
		public Integer scope;
		// classStatus 0：待审核;1:未通过；2，已上架；3，已下架；4，已结束
		public Integer classStatus;
		//一级目录
		public String catalogId1;
		//二级目录
		public String catalogId2;
		//三级目录
		public String catalogId3;
		// 模糊查询  课程名称
		public String name;
		//页面脚标
		public Integer pageIndex=1;
		//页面尺寸
		public Integer pageSize=10;
		
	}

}
